package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.AppCxt;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwdfT;
import cn.e4j.modular.crm.entity.GwfdT;
import cn.e4j.modular.crm.mapper.GwfdTMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  实际费用计算维护 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-10-24
 */
@Service
public class GwfdTServiceImpl extends ServiceImpl<GwfdTMapper, GwfdT> {

    @Transactional(readOnly = true)
    public List<Dto> getGwfdList(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getGwfdList(page, dto);
        return listData;
    }

    /**
     * 获取汇率
     * @return
     */
    public Dto getHuiLv(Dto inDto) {
        Dto huiLv = this.baseMapper.getGwek005(inDto);
        if (huiLv == null) {
            huiLv = Dtos.newDto();
            huiLv.put("GWEK005",1);
        }
        return huiLv;
    }

    @Transactional
    public void addData(List<GwfdT> gwfdTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        //增加新的明细
        String gwdfTime = DateUtils.formatDate(new Date(), "HH:mm:ss");
        for (int i = 0; i < gwfdTList.size(); i++) {
            GwfdT gwfdT = gwfdTList.get(i);
            gwfdT.setGWFDENT(shiroUser.getDBC());
            /*gwfdT.setGWFDUSER(shiroUser.getID());
            gwfdT.setGWFDGRUP(shiroUser.getORG_ID());
            gwfdT.setGWFDDATE(LocalDate.now());
            gwfdT.setGWFDTIME(gwdfTime);
            gwfdT.setGWFDORIG(shiroUser.getORG_ID());
            gwfdT.setGWFDORIU(shiroUser.getID());*/
            gwfdT.insert();
        }
    }

    @Transactional
    public void editData(List<GwfdT> gwfdTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwfdT gwfdTDel = new GwfdT();
        UpdateWrapper gwfdUpdateWrapper = new UpdateWrapper();
        gwfdUpdateWrapper.eq("GWFDENT", shiroUser.getDBC());
        gwfdUpdateWrapper.eq("GWFDDOCNO", gwfdTList.get(0).getGWFDDOCNO());
        gwfdTDel.delete(gwfdUpdateWrapper);
        //增加新的明细
        for (int i = 0; i < gwfdTList.size(); i++) {
            GwfdT gwfdT = gwfdTList.get(i);
            gwfdT.setGWFDMODU(shiroUser.getID());
            gwfdT.insert();
        }
    }

    @Transactional
    public void delete(GwfdT gwfdT) {
        UpdateWrapper<GwfdT> gwdfTUpdateWrapper = new UpdateWrapper<>();
        gwdfTUpdateWrapper.eq("GWFDENT", gwfdT.getGWFDENT());
        gwdfTUpdateWrapper.eq("GWFDDOCNO", gwfdT.getGWFDDOCNO());
        gwfdT.delete(gwdfTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<GwfdT> gwfdTList) {
        for (GwfdT gwfdT : gwfdTList) {
            UpdateWrapper<GwfdT> gwdfTUpdateWrapper = new UpdateWrapper<>();
            gwdfTUpdateWrapper.eq("GWFDENT", gwfdT.getGWFDENT());
            gwdfTUpdateWrapper.eq("GWFDDOCNO", gwfdT.getGWFDDOCNO());
            gwfdT.delete(gwdfTUpdateWrapper);
        }
    }

    @Transactional
    public void updateStatus(List<GwfdT> gwfdTList) {
        for (int i = 0; i < gwfdTList.size(); i++) {
            GwfdT gwfdT = gwfdTList.get(i);
            UpdateWrapper<GwfdT> gwdfTUpdateWrapper = new UpdateWrapper<>();
            gwdfTUpdateWrapper.eq("GWFDENT", gwfdT.getGWFDENT());
            gwdfTUpdateWrapper.eq("GWFDDOCNO", gwfdT.getGWFDDOCNO());
            gwfdT.update(gwdfTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwfdData(Dto dto) {
        List<Dto> listData = this.baseMapper.getGwfdData(dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public Dto getCount(Dto dto) {
        if(!"".equals(dto.getString("GWFD003"))){
            String[] data = dto.getString("GWFD003").split(",");
            List<Dto> gwfd003List = new ArrayList<>();
            for(String GWFD003 : data){
                Dto gwfd = Dtos.newDto();
                gwfd.put("GWFD003", GWFD003);
                gwfd003List.add(gwfd);
            }
            dto.put("gwfd003List",gwfd003List);
        }
        Dto count = this.baseMapper.getCount(dto);
        return count;
    }

    @Transactional
    public Dto autoGeneration(Dto inDto) {
        Dto outDto = Dtos.newDto();
        outDto.put("message", "批次生成成功");
        int amount = Integer.parseInt(AppCxt.getParam("amount_digit_"));
        //根据年度、月份和区域（不选就是全部区域）从订单表抓取已审核的订单数据，按年度、月份、区域、客户、品类、业务组织统计出订单金额
        if(!"".equals(inDto.getString("GWFD003"))){
            String[] data = inDto.getString("GWFD003").split(",");
            List<Dto> gwfd003List = new ArrayList<>();
            for(String GWFD003 : data){
                Dto gwfd = Dtos.newDto();
                gwfd.put("GWFD003", GWFD003);
                gwfd003List.add(gwfd);
            }
            inDto.put("gwfd003List",gwfd003List);
        }
        inDto.put("GWCSDOCDATE", inDto.getString("GWFD001")+ '-' + inDto.getString("GWFD002"));
        List<Dto> gwcsList = this.baseMapper.getGwcsList(inDto);
        List<Dto> gwfaList = this.baseMapper.getGwfaList(inDto);
        String GWFDDOCNO = "FYJS" + DateUtils.getDate("yyyyMMddHHmmssSSS");
        int count = 1;
        if(gwcsList.size() == 0){
            outDto.put("message", "无相关订单数据，未覆盖成功！");
            return outDto;
        }else {
            GwfdT delGwfd = new GwfdT();
            UpdateWrapper<GwfdT> gwdfTUpdateWrapper = new UpdateWrapper<>();
            gwdfTUpdateWrapper.eq("GWFDENT", inDto.getString("ENT"));
            gwdfTUpdateWrapper.eq("GWFD001", inDto.getString("GWFD001"));
            gwdfTUpdateWrapper.eq("GWFD002", inDto.getString("GWFD002"));
            delGwfd.delete(gwdfTUpdateWrapper);
        }
        for (int i=0; i < gwcsList.size(); i++){
            Dto gwcs = gwcsList.get(i);
            for (int j=0; j< gwfaList.size(); j++){
                Dto gwfa = gwfaList.get(j);
                BigDecimal GWDF106 = gwcs.getBigDecimal("GWCS005_SUM").multiply(gwfa.getBigDecimal("GWFA004")).setScale(amount, RoundingMode.HALF_UP); //区域
                BigDecimal GWDF107 = gwcs.getBigDecimal("GWCS005_SUM").multiply(gwfa.getBigDecimal("GWFA004")).setScale(amount, RoundingMode.HALF_UP); //总部
                if(GWDF106.compareTo(new BigDecimal(0)) == 1 && GWDF107.compareTo(new BigDecimal(0)) == 1){
                    Dto qDto = Dtos.newDto();
                    qDto.put("ENT", inDto.getString("ENT"));
                    qDto.put("GWEK001", gwcs.getString("GWAK008"));
                    qDto.put("GWEK002", inDto.getString("GWFD001")+ inDto.getString("GWFD002"));
                    Dto gwek = this.getHuiLv(qDto); //汇率
                    GwfdT gwfdT = new GwfdT();
                    gwfdT.setGWFDENT(inDto.getString("ENT"));
                    gwfdT.setGWFDDOCNO(GWFDDOCNO);
                    gwfdT.setGWFDSEQ(BigDecimal.valueOf(count));
                    gwfdT.setGWFD001(gwcs.getString("GWFD001"));//年度
                    gwfdT.setGWFD002(gwcs.getString("GWFD002"));//月份
                    gwfdT.setGWFD003(gwcs.getString("GWCS023") != null ? gwcs.getString("GWCS023") : "");//区域
                    gwfdT.setGWFD004(gwcs.getString("GWCS010"));//客户
                    gwfdT.setGWFD005(gwcs.getString("GWAM004"));//品类
                    gwfdT.setGWFD006(gwcs.getString("GWCSORIG"));//业务组织
                    gwfdT.setGWFD007(gwfa.getString("GWFA001"));//费用编号
                    gwfdT.setGWFD008("1");//费用来源
                    gwfdT.setGWFD100(gwcs.getString("GWAK008"));//币别
                    gwfdT.setGWFD101(gwek.getBigDecimal("GWEK005"));//汇率
                    gwfdT.setGWFD102(gwcs.getBigDecimal("GWCS005_SUM"));//实际销售原币金额
                    gwfdT.setGWFD103(gwcs.getBigDecimal("GWCS005_SUM").multiply(gwek.getBigDecimal("GWEK005")).setScale(amount, RoundingMode.HALF_UP));//实际销售本币金额
                    gwfdT.setGWFD104(gwfa.getBigDecimal("GWFA004"));//区域费率
                    gwfdT.setGWFD105(gwfa.getBigDecimal("GWFA005"));//总部
                    gwfdT.setGWFD106(GWDF106);//区域原币
                    gwfdT.setGWFD107(GWDF107);//总部原币
                    gwfdT.setGWFD108(GWDF106.multiply(gwek.getBigDecimal("GWEK005")).setScale(amount, RoundingMode.HALF_UP));//区域本币
                    gwfdT.setGWFD109(GWDF107.multiply(gwek.getBigDecimal("GWEK005")).setScale(amount, RoundingMode.HALF_UP));//总部本币
                    gwfdT.setGWFDACTI("N"); //有效码
                    gwfdT.insert();
                    count ++;
                }
            }
        }
        return outDto;
    }

}
